package org.ocean.online.web.workspace;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.wicket.markup.MarkupType;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.ocean.online.utils.CommonUtil;
import org.ocean.online.utils.ZipCompressor;
import org.ocean.online.wicket.NeedLoginPage;

public class DownLoad extends NeedLoginPage{
	private static final long serialVersionUID = 8559002067431754867L;

	public DownLoad(){
		
	}
	
	@Override
	public MarkupType getMarkupType() {
		MarkupType type = new MarkupType(".zip","application/zip");
		return type;
	}

	
	@Override
	public void renderPage() {
		File file = new File(this.getWorkspace());
		HttpServletRequest servletRequest = ((ServletWebRequest) getRequest()).getContainerRequest();
		List<File> list = new ArrayList<File>();
		Collections.addAll(list, file.listFiles());
		File file2 = new File(servletRequest.getRealPath("/"));
		Collections.addAll(list, file2.listFiles());
//		list.add(file2);
		//list.add(new File(servletRequest.getRealPath("/")));
		HttpServletResponse httpResponse = (HttpServletResponse) this.getResponse().getContainerResponse();
		httpResponse.reset();
		httpResponse.setBufferSize(8192);
		httpResponse.setContentType("application/zip");//根据实际情况写传输的数据类型  
		double size = CommonUtil.getDirSize(file) + CommonUtil.getDirSize(file2);
		//httpResponse.setContentLength((int) size);
		httpResponse.addHeader("Content-Disposition", "attachment; filename=\""  
		             +this.getUsername()+ ".war");
		
		try {
//			ZipCompressor zip = new ZipCompressor();
			ZipCompressor.compressFiles(list.toArray(new File[]{}), httpResponse.getOutputStream(), "");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
